home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9941 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.1 KB  |  165 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!ind-007-237-41
  3. From: dlmiller@iquest.net (Doug Miller)
  4. Subject: Re: PLEASE Help! :(
  5. X-Nntp-Posting-Host: ind-007-237-41.iquest.net
  6. Message-ID: <Do9GMv.BKF@iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Network Services
  9. X-Newsreader: News Xpress Version 1.0 Beta #2.1
  10. References: <4hsqk2$4d7@deadbird.db.erau.edu>
  11. Date: Thu, 14 Mar 1996 14:11:02 GMT
  12.  
  13. kochank@news.db.erau.edu (Konrad Kochan) wrote:
  14. +Xref: news1 comp.lang.c:70770
  15. +Path: news1!news.sprintlink.net!news.aus.sig.net!news.cfi.org!zilker.net!halley!news.mpd.tandem.com!tandem!pacbell.com!decwrl!pa.dec.com!decuac.dec.com!haven.umd.edu!darwin.sura.net!news.db.erau.edu!kochank
  16. +From: kochank@news.db.erau.edu (Konrad Kochan)
  17. +Newsgroups: comp.lang.c
  18. +Subject: PLEASE Help! :(
  19. +Date: 9 Mar 1996 20:44:50 GMT
  20. +Organization: Embry-Riddle Aeronautical University
  21. +Lines: 107
  22. +Message-ID: <4hsqk2$4d7@deadbird.db.erau.edu>
  23. +NNTP-Posting-Host: 155.31.1.1
  24. +NNTP-Posting-User: kochank
  25. +X-Newsreader: TIN [version 1.2 PL2]
  26. +
  27. +Howdy.. I have a program to write, but I'm clueless... Now I could
  28. +give you a 100 excuses and blame the instructor (and I should cause
  29. +all of it would be true.. :)
  30. +
  31. +Anyways, I'm clueless.. can someone, a good samaritan (or someone just
  32. +bored..) help me out!
  33. +
  34. +Here is the program statement..
  35. +
  36. +Write a C program to sort, using any sorting method, a text file
  37. +named RECORDS.DAT, of unknown length, of records organized as follows:
  38. +
  39. +Field 1.  Contains an integer.
  40. +Field 2.  Contains a string of exactly 20 characters.
  41. +Field 3.  Contains a float followed by a newline character.
  42. +
  43. +Example of file organization:
  44. +
  45. +234 Numa Chaikin        33000.0
  46. + 75 advertisements      55.55
  47. +901 Shepard             15255.5
  48. +  3 Brook               120050.0
  49. +888 non-descriptive     0.0
  50. +
  51. +Sorting has to be done lexicographically, that is, by the second field
  52. +using alphabetical order.  This means that:
  53. +
  54. +1. Upper-case and lower-case letters should be treated identically.
  55. +   For example, the right order for a part of the list above is:
  56. +   888 non-descriptive 0.0
  57. +   234 Numa Chaikin 33000.0
  58. +
  59. +2. The only punctuation sign used in strings is a dash '-', which
  60. +   should be ignored in sorting (but not removed from words).
  61. +And here is what I got so far.. which DOESN'T work (right or otherwise, I
  62. +can't even get the program to scan the right fields from the file.. :(
  63. +
  64. +#include <stdio.h>
  65. +#include <stdlib.h>
  66. +#include <string.h>
  67. +
  68. +/* Global Variable Definition */
  69. +
  70. +int IntArr[512][5];
  71. +char WordsArr1[512][10];
  72. +char WordsArr2[512][10];
  73. +float FloatArr[512][5];
  74. +char inbuff[80];
  75. +int index=0;
  76. +
  77. +/* Function declaration */
  78. +
  79. +void Welcome(void);
  80. +void Goodbye(void);
  81. +
  82. +main()
  83. +{
  84. +/* Next function opens a file, if the file is missing, it prints
  85. + an error message */
  86. +
  87. +int i;
  88. + FILE * fin, * fout;
  89. +    if (!(fin = fopen("RECORDS.DAT", "r")))
  90. +     {
  91.  
  92. don't EVER give your users a horseshit error message like this.
  93.  
  94. +    printf("Something is wrong: Probably file missing.\n");
  95.  
  96. use perror () to find out and display the *exact* problem.
  97.  
  98.  
  99. +    exit(1);   /* Program exits here if file is missing. */
  100. +            }
  101. +    printf("\nInput File = RECORDS.DAT\n");
  102. +/* This function prints a Welcome message */
  103. +
  104. +/* Welcome(); */
  105. +
  106. +/* This function scans in the array */
  107. +
  108. +    while (fgets(inbuff,80,fin) !=NULL)
  109. +{
  110.  
  111. This sscanf statement contains most of your problems:
  112.  
  113. +sscanf(inbuff,"%d %s %s %4.2f", &IntArr[index],&WordsArr1[index],&WordsArr2[index],&FloatArr[index]);
  114.  
  115. 1) by including spaces in your format specifiers, you have instructed the program to *demand*
  116. at least one space between each field, and it *will* fail if there isn't.
  117. 2) you are also *demanding* two strings between the decimal and the float, in contradiction to
  118. the program specification of one string. sscanf will fail if only one string is present.
  119. 3) your format %4.2f is arbitrary, justified neither by the terms of the assignment nor by the
  120. sample data you provided. This will also produce undesirable program behavior if the input
  121. field is too long.
  122. 4) the program specification is for a string of exactly 20 characters in the second field. You are
  123. not looking for a string of exactly 20 characters.
  124.  
  125.  
  126. +index++;
  127. +}
  128. +
  129. +/* Opens the output file */
  130. +
  131. +if (!(fout = fopen("test", "w")))
  132. +     {
  133. +    printf("Something is wrong: File might be write protected.\n");
  134. +    exit(1);   /* Program exits here if file can't be opened. */
  135. +            }
  136. +
  137. +    for (i=0; i<index; i++)
  138. +    fprintf(fout, "%d %s %s %4.2f\n",  IntArr[i], WordsArr1[i],WordsArr2[i],
  139. +     FloatArr[i]);
  140. +
  141. +/* Prints the sorted array to the screen */
  142.  
  143. excuse me?  where did it get sorted?
  144.  
  145. +
  146. +    for (i=0; i<index; i++)
  147. +    printf("Original array: %d %s %s %4.2f \n", IntArr[i],WordsArr1[i],
  148. +WordsArr2[i],FloatArr[i]);
  149. +fclose(fin);
  150. +fclose(fout);
  151. +}
  152. +If you can either give me hints, rewrite the above, or write a totaly
  153. +new program I would appreciate it.. I know its a lot to ask for (to do some-
  154. +one elses homework pretty much..) but I'm really clueless..
  155. +
  156.  
  157. You're right, it *is* a lot to ask for.
  158.  
  159. hints, gladly. (see above)
  160.  
  161. rewrite it? or write you a new program?  forget it, pal.  you've gotta be kidding.
  162. you have to do this yourself if you expect to learn anything.
  163.  
  164.  
  165.